package com.example.userclient.Main

import android.graphics.Bitmap
import android.os.Handler
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.BindingAdapter
import com.baidu.mapapi.map.MyLocationData
import com.baidu.mapapi.model.LatLng
import com.example.userclient.*
import com.example.userclient.custom.CustomCircleImageView
import com.example.userclient.custom.CustomImageView
import com.example.userclient.design.Observer
import com.example.userclient.design.Subject
import com.example.userclient.remote.AppExecutors
import com.example.userclient.remote.Requester
import java.lang.ref.SoftReference
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentSkipListSet
import kotlin.collections.ArrayList

object ClientInfoHolder : Subject{
    //如果超过 一定天数的费用计算
    val GET_DATA_SUCCESS = 1
    val NETWORK_ERROR = 2
    val SERVER_ERROR = 3
    val  map  = ConcurrentHashMap<String, SoftReference<Bitmap>>()
    private val observers =Vector<Observer>()

    //机箱识别号


    var readRemark: ReadRemark? = ReadRemark("", Collections.synchronizedList(ArrayList()))

    var locationDescribe:String? = null


    var clientMap :Map<String, HeartBeatInfo> ?=null

    var clientMapWithin1000 :ArrayList<HeartBeatInfo> ?=null


    var userAddBook :Set<AddBookExtension> ?=ConcurrentSkipListSet<AddBookExtension>()

    var userBuyBook :Set<AddBookExtension> ?=ConcurrentSkipListSet<AddBookExtension>()

    var liveBook :Set<AddBook> ?= ConcurrentSkipListSet<AddBook>()

    var phone:String?=null

    // 位置信息
    var position : Position?=null

    var phone_bitmap : Bitmap? =null

    var myloc: MyLocationData? = null

    var ll :LatLng?= null


    var personRemark:MutableList<BookRemark>? = LinkedList()


    var books : List<SaleBook> ?= null

    fun loadNetBook(){
        AppExecutors.startTask(
                Thread {
                    val result = Requester.apiService().getRegisterClient().execute()
                    if (result.isSuccessful) {
                        clientMap = result.body()!!.data as Map<String, HeartBeatInfo>
                    }
                    val result2 = Requester.apiService().getUserBooks().execute()
                    if (result2.isSuccessful) {
                        liveBook = result2.body()!!.data as Set<AddBook>
                    }
                    phone?.let {
                        val result3 = Requester.apiService().getUserBuyBooks(phone!!).execute()
                        userBuyBook = result3.body()!!.data as Set<AddBookExtension>
                    }
                    phone?.let {
                        val result4 = Requester.apiService().getUserSaleBooks(phone!!).execute()
                        userAddBook = result4.body()!!.data as Set<AddBookExtension>
                    }
                }
        )
    }

    fun loadNetBook(handler: Handler){
        AppExecutors.startTask(
                Thread {
//                     while (true){
                         val result = Requester.apiService().getRegisterClient().execute()
                         if (result.isSuccessful) {
                             clientMap = result.body()!!.data as Map<String, HeartBeatInfo>
                         }
                         val result2 = Requester.apiService().getUserBooks().execute()
                         if (result2.isSuccessful) {
                             liveBook = result2.body()!!.data as Set<AddBook>
                         }
                         phone?.let {
                             val result3 = Requester.apiService().getUserBuyBooks(phone!!).execute()
                             userBuyBook = result3.body()!!.data as Set<AddBookExtension>
                         }

                         phone?.let {
                             val result4 = Requester.apiService().getUserSaleBooks(phone!!).execute()
                             userAddBook = result4.body()!!.data as Set<AddBookExtension>
                         }
                         handler.sendEmptyMessage(GET_DATA_SUCCESS)
                         Thread.sleep(1000)
//                     }
                }
        )
    }

    override fun attach(observer: Observer) {

        synchronized(observers){
            observers.addElement(observer)
        }

    }

    override  fun   detach(observer: Observer) {
        synchronized(observers){
            observers.remove(observer)
        }

    }

    override fun notifyChanged() {

        var objects : Array<Any>

        synchronized(observers){

            objects = observers.toArray()
        }

        for (t in objects) {
            (t as Observer).update(this)
        }
    }

    @BindingAdapter("src")
    @JvmStatic
     fun setBookPicture(view: CustomCircleImageView, addBook: AddBook) {
        Log.i("CustomCircleImageView", "setBookPicture")
//        Glide.with(MyApplication.instance).load(saleBook.picture).into(view)
        view.setImageURL(addBook.picture)
    }

    @BindingAdapter("src")
    @JvmStatic
    fun setBookPicture(view: CustomImageView, addBook: AddBook) {
        Log.i("CustomImageView", "setBookPicture")
//        Glide.with(MyApplication.instance).load(saleBook.picture).into(view)
        view.setImageURL(addBook.picture)
    }

    @BindingAdapter("bitmap")
    @JvmStatic
    fun setBookPicture(view: ImageView, bitmap: Bitmap?) {
        Log.i("ImageView", "$bitmap")
//        Glide.with(MyApplication.instance).load(saleBook.picture).into(view)
//        view.setImageURL(addBook.picture)
        if (bitmap==null){
//            view.setImageBitmap(bitmap)
            return
        }
        view.setImageBitmap(bitmap)
    }

    @BindingAdapter("visibility")
    @JvmStatic
    fun isVisibility(view: View, sendFrom: String) {
//        Log.i("TalkListAdapter","setVisibility")
        if (        view.id == R.id.left_layout){
            if (sendFrom == phone){
                 view.visibility = View.GONE
            }else{
                view.visibility = View.VISIBLE
            }
        }else if (  view.id == R.id.right_layout){
            if (sendFrom == phone){
                view.visibility = View.VISIBLE
            }else{
                view.visibility = View.GONE
            }
        }
    }
    @BindingAdapter("isRead")
    @JvmStatic
    fun isRead(view: ImageView, bookRemark: BookRemark) {

        if (bookRemark.sendTo == bookRemark.sendFrom||bookRemark.sendTo==ClientInfoHolder.phone){
            view.visibility = View.GONE
            return
        }
        val isRead:Boolean = bookRemark.isRead
        Log.i("TalkListAdapter", "$isRead")
        if (isRead){
            view.visibility = View.GONE
        }else{
            view.visibility = View.VISIBLE
        }
    }

    @BindingAdapter("isReply")
    @JvmStatic
    fun isReply(view: ImageView, phone: String) {

//        if (bookRemark.sendTo == bookRemark.sendFrom||bookRemark.sendTo!=ClientInfoHolder.phone){
//            view.visibility = View.GONE
//            return
//        }
        val isRead:Boolean = phone == ClientInfoHolder.phone
        if (isRead){
            view.visibility = View.GONE
        }else{
            view.visibility = View.VISIBLE
        }
    }

    @BindingAdapter("isShow")
    @JvmStatic
    fun isShow(view: TextView, isShow: Boolean) {

//        if (bookRemark.sendTo == bookRemark.sendFrom||bookRemark.sendTo!=ClientInfoHolder.phone){
//            view.visibility = View.GONE
//            return
//        }
        if (!isShow){
            view.visibility = View.GONE
        }else{
            view.visibility = View.VISIBLE
        }
    }
}